package service

import (
	"context"
	"fmt"
	"iqiyi-srv/basic/config"
	__ "iqiyi-srv/basic/proto"
	"iqiyi-srv/handler/model"
	"strconv"
	"time"
)

type Server struct {
	__.UnimplementedIQiYiServer
}

func (s *Server) SendSms(_ context.Context, in *__.SendSmsReq) (*__.SendSmsResp, error) {

	//检测
	get := config.RDB.Get(config.Ctx, "sendSms"+in.Mobile+"count")
	//请求验证码5次
	if get.Val() >= strconv.Itoa(5) {
		//设置24小时后过期
		config.RDB.Expire(config.Ctx, "sendSms"+in.Mobile+"count", time.Hour*24)
		return nil, fmt.Errorf("同一手机号每天最多请求验证码5次")
	}

	//短信接口
	//pkg.SendSms(in.Mobile, in.Code)

	//缓存短信验证码-5分钟过期
	config.RDB.Set(config.Ctx, "sendSms"+in.Mobile+in.Source, in.Code, time.Minute*5)
	config.RDB.Incr(config.Ctx, "sendSms"+in.Mobile+"count") //发送次数累加

	return &__.SendSmsResp{}, nil
}

func (s *Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {

	//验证码
	get := config.RDB.Get(config.Ctx, "sendSms"+in.Mobile+"register")
	if get.Val() != in.Sms {
		return nil, fmt.Errorf("验证码输入错误")
	}

	var user model.User

	user = model.User{
		Mobile:       in.Mobile,
		Password:     in.Password,
		Nickname:     in.Nickname,
		ProtobufTime: time.Now(),
	}

	err := user.CreateUser()
	if err != nil {
		return nil, err
	}

	return &__.CreateUserResp{UserId: int64(user.Id)}, nil
}

func (s *Server) LoginUser(_ context.Context, in *__.LoginUserReq) (*__.LoginUserResp, error) {

	var user model.User

	user.Mobile = in.Mobile
	err := user.MobileGetUser()
	if err != nil {
		return nil, err
	}

	if user.Id == 0 {
		return nil, fmt.Errorf("账号不存在")
	}

	//验证码-登陆
	get := config.RDB.Get(config.Ctx, "sendSms"+in.Mobile+"login")
	if get.Val() != in.Sms {
		return nil, fmt.Errorf("验证码输入错误")
	}

	return &__.LoginUserResp{UserId: int64(user.Id)}, nil
}

func (s *Server) AddVideo(_ context.Context, in *__.AddVideoReq) (*__.AddVideoResp, error) {

	var video model.Video

	video = model.Video{
		Title:     in.Title,
		Info:      in.Info,
		CateTag:   in.CateTag,
		VipStatus: int8(in.VipStatus),
		Image:     in.Image,
		VideoUrl:  in.VideoUrl,
		Status:    int8(in.Status),
	}
	err := video.AddVideo()
	if err != nil {
		return nil, err
	}

	return &__.AddVideoResp{VideoId: int64(video.Id)}, nil
}

func (s *Server) Protobuf(_ context.Context, in *__.ProtobufReq) (*__.ProtobufResp, error) {

	var protobuf model.Protobuf

	protobuf = model.Protobuf{
		VideoId: int32(in.VideoId),
		Status:  int8(in.Status),
		Comment: in.Comment,
	}

	err := protobuf.UpdateProto()
	if err != nil {
		return nil, err
	}

	return &__.ProtobufResp{ProtobufId: int64(protobuf.Id)}, nil
}

func (s *Server) KeywordVideo(_ context.Context, in *__.KeywordVideoReq) (*__.KeywordVideoResp, error) {

	var video model.Video

	keywordVideo, err := video.KeywordVideo(in.Keyword)
	if err != nil {
		return nil, err
	}

	/*
		marshal, err := json.Marshal(keywordVideo)
		if err != nil {
			return nil, err
		}


		config.RDB.Set(config.Ctx, "video", marshal, time.Hour*12)
	*/

	return &__.KeywordVideoResp{List: keywordVideo}, nil
}

func (s *Server) InfoVideo(_ context.Context, in *__.InfoVideoReq) (*__.InfoVideoResp, error) {

	var video model.Video

	infoVideo, err := video.InfoVideo(int(in.VideoId))
	if err != nil {

		return nil, err
	}

	return &__.InfoVideoResp{List: infoVideo}, nil
}

func (s *Server) AddUSerVideoIngo(_ context.Context, in *__.AddUSerVideoIngoReq) (*__.AddUSerVideoIngoResp, error) {

	var userVideoLog model.UserVideoLog
	userVideoLog = model.UserVideoLog{
		UserId:   int32(in.UserId),
		VideoId:  int32(in.VideoId),
		LookTime: time.Time{},
	}

	err := userVideoLog.AddUserVideoLog()
	if err != nil {
		return nil, err
	}

	return &__.AddUSerVideoIngoResp{VideoLogId: int64(userVideoLog.Id)}, nil
}

func (s *Server) AddUserLikeVideo(_ context.Context, in *__.AddUserLikeVideoReq) (*__.AddUserLikeVideoResp, error) {

	var userLike model.UserLike
	userLike = model.UserLike{
		VideoId: int32(in.VideoId),
		UserId:  int32(in.UserId),
	}

	err := userLike.AddLike()
	if err != nil {
		return nil, err
	}
	return &__.AddUserLikeVideoResp{UserLikeId: int64(userLike.Id)}, nil
}

func (s *Server) GinUserLikeVideo(_ context.Context, in *__.GetUserLikeVideoReq) (*__.GetUserLikeVideoResp, error) {

	var userLike model.UserLike

	like, err := userLike.GetUserLike(in.UserId)
	if err != nil {
		return nil, err
	}

	return &__.GetUserLikeVideoResp{List: like}, nil
}

func (s *Server) GetUserVipList(_ context.Context, in *__.GetUserVipListReq) (*__.GetUserVipListResp, error) {

	var userVip model.UserVip
	vip, err := userVip.GetUserVip()
	if err != nil {
		return nil, err
	}

	return &__.GetUserVipListResp{List: vip}, nil
}
